<%@page import="org.apache.solr.request.QueryResponseWriter"%>
<%@page import="org.apache.solr.request.SolrQueryResponse"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.solr.client.solrj.response.FacetField"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.solr.common.SolrDocumentList"%>
<%@page import="org.apache.solr.client.solrj.response.QueryResponse"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link type="text/css"  rel ="stylesheet"  href="/SolrQMSApplication/css/SolrQms.css">
<script type="text/javascript" >
function callFacetList( anchorElement )
{
	try {
		var urlToPass = anchorElement.getAttribute("CUSTOM");
		parent.document.getElementById("detail").src = urlToPass;
	} catch (e) {
		alert("Exception: " + e);
	}
}
</script>
</head>
<body>
<%
	HttpSession solrSession= request.getSession();
	QueryResponse queryResponse = (QueryResponse) solrSession.getAttribute("solrResponse");
	String queryString = (String) solrSession.getAttribute("queryString");
	String uri=(String) solrSession.getAttribute("requestUri");
	List<FacetField> facetFields = queryResponse.getFacetFields();
	Iterator<FacetField> facetFieldIterator = facetFields.iterator();
	System.out.println("hello [ "+facetFields.toString()+" ]");
	String tmpUrl ="";
	while(facetFieldIterator.hasNext()) 
	{
		FacetField facetField = facetFieldIterator.next(); 
%>
<BR><li><%=facetField.getName() %></li><ul> 
<% 	
		List<FacetField.Count> facetFieldCounts = facetField.getValues();
		System.out.println("facetFieldCounts :[ "+facetFieldCounts+" ]");
		Iterator<FacetField.Count> facetFieldCountIterator = facetFieldCounts.iterator();
		System.out.println("facetFieldCountIterator :[ "+facetFieldCountIterator+" ]");
		boolean hasQueryString = false;
		while(facetFieldCountIterator.hasNext())
		{
			FacetField.Count count = facetFieldCountIterator.next();
			StringBuffer stringBuffer = new StringBuffer("?");
			if(queryString != null) 
			{
				stringBuffer.append(queryString);
				hasQueryString = true;
			}
			if(hasQueryString) 
			{
				stringBuffer.append("&");
			}
			stringBuffer.append(facetField.getName() + "=" + count.getName());
			//System.out.println("@@@@ string "+uri + stringBuffer.toString());
			//System.out.println("@@@@ request.getRequestURI() in tld : [ "+uri+" ]");
			//System.out.println("@@@@ queryString :"+queryString);
			
			String removeLink = null;
			if(null != queryString) 
			{
				removeLink = uri + "?" + queryString;
				removeLink = removeLink.replaceAll(facetField.getName() + "=" + count.getName(), "");
			}
				
			if(null != queryString && queryString.contains(facetField.getName() + "=" + count.getName())) 
			{
			//	System.out.println("Inside if...");
				
%>
<li><%= count.getName() %>(<%=count.getCount()%>)<a href="<%=removeLink%>">(remove)</a></li>
<% 			} 
			else 
			{
			//	System.out.println("Inside else...");
				if(count.getCount() > 0 )
				{	
					tmpUrl = uri + stringBuffer.toString();
				//	System.out.println(tmpUrl);
%>
<li><a onclick=callFacetList(this); href="#" CUSTOM="<%=tmpUrl%>"><%= count.getName() %>( <%=count.getCount() %>)</a></li>
<%		
				}	
			}
				
		}
%>		
</ul></li>
<%	
	}
%>
</body>
</html>